package com.irdstudio.efp.nls.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;
import com.irdstudio.efp.nls.service.facade.NlsApplyRelationService;
import com.irdstudio.efp.nls.service.vo.NlsApplyRelationVO;

@RestController
@RequestMapping("/api")
public class NlsApplyRelationController extends AbstractController  {
	
	@Autowired
	@Qualifier("nlsApplyRelationService")
	private NlsApplyRelationService nlsApplyRelationService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	/**
	 * 列表数据查询
	 * @return
	 */
	@RequestMapping(value="/nls/apply/relations", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<NlsApplyRelationVO>> queryNlsApplyRelationAll(
			@RequestBody NlsApplyRelationVO vo) {		
//		List<NlsApplyRelationVO> outputVo = this.executeQueryList("queryAll", nlsApplyRelationService, new Object[]{vo});
		setUserInfoToVO(vo);
		List<NlsApplyRelationVO> outputVo = null;
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = nlsApplyRelationService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = nlsApplyRelationService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = nlsApplyRelationService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = nlsApplyRelationService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/nls/apply/relation/{applySeq}/{cusId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<NlsApplyRelationVO> queryByPk(@PathVariable("applySeq") String applySeq,@PathVariable("cusId") String cusId) {		
		NlsApplyRelationVO inVo = new NlsApplyRelationVO();
		inVo.setApplySeq(applySeq);
		inVo.setCusId(cusId);
		NlsApplyRelationVO outputVo = nlsApplyRelationService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @return
	 */
	@RequestMapping(value="/nls/apply/relation", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody NlsApplyRelationVO inNlsApplyRelationVo) {		
		int outputVo = nlsApplyRelationService.deleteByPk(inNlsApplyRelationVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inNlsApplyRelationVo
	 * @return
	 */
	@RequestMapping(value="/nls/apply/relation", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody NlsApplyRelationVO inNlsApplyRelationVo) {		
		int outputVo = nlsApplyRelationService.updateByPk(inNlsApplyRelationVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inNlsApplyRelationVo
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/nls/apply/relation", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertNlsApplyRelation(@RequestBody NlsApplyRelationVO inNlsApplyRelationVo) throws Exception {
		int outputVo = nlsApplyRelationService.insertNlsApplyRelation(inNlsApplyRelationVo);
		return getResponseData(outputVo);
		
	}

	/**
	 * 关联联系人查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/nls/apply/relations/rel", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<NlsApplyRelationVO>> queryNlsApplyRelationRelAll(@RequestBody NlsApplyRelationVO vo) {
		List<NlsApplyRelationVO> outputVo = nlsApplyRelationService.queryNlsApplyRelationRelAll(vo);
		return getResponseData(outputVo);

	}
}
